// stdafx.cpp : source file that includes just the standard includes
//	DownloadPro.pch will be the pre-compiled header
//	stdafx.obj will contain the pre-compiled type information

#include "stdafx.h"
#include <time.h>
#include <Shlwapi.h>

void WriteLogFile(CString logMsg)
{
	CStdioFile csFile;
	CString strPath;
	TCHAR szFull[_MAX_PATH];
	TCHAR szDrive[_MAX_DRIVE];
	TCHAR szDir[_MAX_DIR];
	::GetModuleFileName(NULL, szFull, sizeof(szFull)/sizeof(TCHAR));
	_tsplitpath(szFull, szDrive, szDir, NULL, NULL);
	_tcscpy(szFull, szDrive);
	_tcscat(szFull, szDir);
	strPath = CString(szFull);
	strPath += "\\error.log";
	csFile.Open(strPath,CFile::modeCreate|CFile::modeWrite|CFile::modeNoTruncate);
	csFile.SeekToEnd();

	struct tm * logTime;
	char strLogTimes[128]; 
	memset(strLogTimes,0,sizeof(strLogTimes));
	time_t nowTime; 
	nowTime = time(NULL); 
	logTime = localtime(&nowTime); 
	strftime(strLogTimes, 128, "%Y-%m-%d %H:%M:%S\n", logTime);
	CString msgTime = "";
	msgTime.Format("%s",strLogTimes);

	csFile.WriteString(msgTime + ": " + logMsg + "\n");
	csFile.Close();
}

CString ReadConfigFile()
{
	CStdioFile csFile;
	CString strPath;
	TCHAR szFull[_MAX_PATH];
	TCHAR szDrive[_MAX_DRIVE];
	TCHAR szDir[_MAX_DIR];
	::GetModuleFileName(NULL, szFull, sizeof(szFull)/sizeof(TCHAR));
	_tsplitpath(szFull, szDrive, szDir, NULL, NULL);
	_tcscpy(szFull, szDrive);
	_tcscat(szFull, szDir);
	strPath = CString(szFull);
	strPath += "\\config.ini";
	if (!csFile.Open(strPath,CFile::modeRead))
	{
		csFile.Close();
		CString configInfo;
		TCHAR szFull[_MAX_PATH];
		TCHAR szDrive[_MAX_DRIVE];
		TCHAR szDir[_MAX_DIR];
		::GetWindowsDirectory(szFull, sizeof(szFull)/sizeof(TCHAR));
		_tsplitpath(szFull, szDrive, szDir, NULL, NULL);
		_tcscpy(szFull, szDrive);
		_tcscat(szFull, szDir);
		configInfo = CString(szFull);
		configInfo += "AQBD";
		WriteConfigFile(configInfo);
		if (!PathIsDirectory(configInfo))
		{
			CreateDirectory(configInfo,NULL);
		}
		return configInfo;
	}

	CString configInfo;
	csFile.ReadString(configInfo);
	if (!PathIsDirectory(configInfo))
	{
		csFile.Close();
		configInfo = "";
		TCHAR szFull[_MAX_PATH];
		TCHAR szDrive[_MAX_DRIVE];
		TCHAR szDir[_MAX_DIR];
		::GetWindowsDirectory(szFull, sizeof(szFull)/sizeof(TCHAR));
		_tsplitpath(szFull, szDrive, szDir, NULL, NULL);
		_tcscpy(szFull, szDrive);
		_tcscat(szFull, szDir);
		configInfo = CString(szFull);
		configInfo += "AQBD";
		WriteConfigFile(configInfo);
		CreateDirectory(configInfo,NULL);
	} else
	{
		csFile.Close();
	}
	
	return configInfo;
}

void WriteConfigFile(CString str)
{
	CStdioFile csFile;
	CString strPath;
	TCHAR szFull[_MAX_PATH];
	TCHAR szDrive[_MAX_DRIVE];
	TCHAR szDir[_MAX_DIR];
	::GetModuleFileName(NULL, szFull, sizeof(szFull)/sizeof(TCHAR));
	_tsplitpath(szFull, szDrive, szDir, NULL, NULL);
	_tcscpy(szFull, szDrive);
	_tcscat(szFull, szDir);
	strPath = CString(szFull);
	strPath += "\\config.ini";
	csFile.Open(strPath,CFile::modeCreate | CFile::modeWrite);
	csFile.WriteString(str);
	csFile.Close();
}

BOOL IsFirstRUN(CString FileName)
{
	BOOL flag = TRUE;
	CString strPath;
	TCHAR szFull[_MAX_PATH];
	TCHAR szDrive[_MAX_DRIVE];
	TCHAR szDir[_MAX_DIR];
	::GetModuleFileName(NULL, szFull, sizeof(szFull)/sizeof(TCHAR));
	_tsplitpath(szFull, szDrive, szDir, NULL, NULL);
	_tcscpy(szFull, szDrive);
	_tcscat(szFull, szDir);
	strPath = CString(szFull);
	strPath += "\config.ini";
	
	if (PathFileExists(strPath))
	{
		CString DownlaodFile = ReadConfigFile();
		DownlaodFile += "\\" + FileName;
		if (PathFileExists(DownlaodFile))
		{
			flag = FALSE;
		}

	}

	return flag;
}


